1
|
|
|
/* |
2
|
|
|
* from-bytes: convert bytes to numbers and strings. |
3
|
|
|
* Copyright (c) 2017 Rafael da Silva Rocha. |
4
|
|
|
* https://github.com/rochars/byte-data |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
const reader = require("../src/read-bytes.js"); |
8
|
|
|
const bitDepths = require("../src/bit-depth.js"); |
9
|
|
|
const helpers = require("../src/helpers.js"); |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Turn a byte buffer into what the bytes represent. |
13
|
|
|
* @param {!Array<number>|Uint8Array} buffer An array of bytes. |
14
|
|
|
* @param {number} bitDepth The bit depth of the data. |
15
|
|
|
* Possible values are 1, 2, 4, 8, 16, 24, 32, 40, 48 or 64. |
16
|
|
|
* @param {Object} options The options. They are: |
17
|
|
|
* - "signed": If the numbers are signed. Default is false (unsigned). |
18
|
|
|
* - "float": True for floating point numbers. Default is false. |
19
|
|
|
* This option is available for 16, 32 and 64-bit numbers. |
20
|
|
|
* - "base": The base of the input. Default is 10. Can be 2, 10 or 16. |
21
|
|
|
* - "char": If the bytes represent a string. Default is false. |
22
|
|
|
* - "be": If the values are big endian. Default is false (little endian). |
23
|
|
|
* - "single": If it should return a single value instead of an array. |
24
|
|
|
* Default is false. |
25
|
|
|
* @return {!Array<number>|string} |
26
|
|
|
*/ |
27
|
|
|
function fromBytes(buffer, bitDepth, options={"base": 10}) { |
28
|
|
|
helpers.makeBigEndian(buffer, options.be, bitDepth); |
29
|
|
|
helpers.bytesToInt(buffer, options.base); |
30
|
|
|
let values = readBytes( |
31
|
|
|
buffer, |
32
|
|
|
bitDepth, |
33
|
|
|
options.signed, |
34
|
|
|
getBitReader(bitDepth, options.float, options.char) |
35
|
|
|
); |
36
|
|
|
if (options.char) { |
37
|
|
|
values = values.join(""); |
38
|
|
|
} |
39
|
|
|
if (options.single) { |
40
|
|
|
values = values[0]; |
41
|
|
|
} |
42
|
|
|
return values; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Turn a array of bytes into an array of what the bytes should represent. |
47
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
48
|
|
|
* @param {number} bitDepth The bitDepth. 1, 2, 4, 8, 16, 24, 32, 40, 48, 64. |
49
|
|
|
* @param {boolean} isSigned True if the values should be signed. |
50
|
|
|
* @param {Function} bitReader The function to read the bytes. |
51
|
|
|
* @return {!Array<number>|string} |
52
|
|
|
*/ |
53
|
|
|
function readBytes(bytes, bitDepth, isSigned, bitReader) { |
54
|
|
|
let values = []; |
55
|
|
|
let i = 0; |
56
|
|
|
let j = 0; |
57
|
|
|
let offset = bitDepths.BitDepthOffsets[bitDepth]; |
58
|
|
|
let len = bytes.length - (offset -1); |
59
|
|
|
let maxBitDepthValue = bitDepths.BitDepthMaxValues[bitDepth]; |
60
|
|
|
let signFunction = isSigned ? helpers.signed : function(x,y){return x;}; |
|
|
|
|
61
|
|
|
while (i < len) { |
62
|
|
|
values[j] = signFunction(bitReader(bytes, i), maxBitDepthValue); |
63
|
|
|
i += offset; |
64
|
|
|
j++; |
65
|
|
|
} |
66
|
|
|
return values; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Return a function to read binary data. |
71
|
|
|
* @param {number} bitDepth The bitDepth. 1, 2, 4, 8, 16, 24, 32, 40, 48, 64. |
72
|
|
|
* @param {boolean} isFloat True if the values are IEEE floating point numbers. |
73
|
|
|
* @param {boolean} isChar True if it is a string. |
74
|
|
|
* @return {Function} |
75
|
|
|
*/ |
76
|
|
|
function getBitReader(bitDepth, isFloat, isChar) { |
77
|
|
|
let bitReader; |
78
|
|
|
if (isChar) { |
79
|
|
|
bitReader = reader.readChar; |
80
|
|
|
} else { |
81
|
|
|
bitReader = reader[getReaderFunctionName(bitDepth, isFloat)]; |
82
|
|
|
} |
83
|
|
|
return bitReader; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Build a bit reading function name based on the arguments. |
88
|
|
|
* @param {number} bitDepth The bitDepth. 1, 2, 4, 8, 16, 24, 32, 40, 48, 64. |
89
|
|
|
* @param {boolean} isFloat True if the values are IEEE floating point numbers. |
90
|
|
|
* @return {string} |
91
|
|
|
*/ |
92
|
|
|
function getReaderFunctionName(bitDepth, isFloat) { |
93
|
|
|
return 'read' + |
94
|
|
|
((bitDepth == 2 || bitDepth == 4) ? 8 : bitDepth) + |
95
|
|
|
'Bit' + |
96
|
|
|
(isFloat ? "Float" : ""); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
module.exports.fromBytes = fromBytes; |
100
|
|
|
|
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.